//# sourceURL=http://uMapApp/UI/QueryManager
define([
    "mhjs/mhjs"
    , "./QueryNode"
    , "./ModalityLoading"
], function (
    mhjs,
    QueryNode,
    ModalityLoading
) {

    function QueryManager(option) {

        var _this = this;
        _this.isOpened = false;
        _this.modalityQueries = [];
        _this.modalityLoading = new ModalityLoading();

        mhjs.Div.call(_this, option);
        _this.addClass("queryManager");

    }

    var _class = QueryManager;
    mhjs.Inherit(_class, mhjs.Div);


    Object.defineProperties(_class.prototype, {
        "add"                 : {
            value: function (queryNode) {

                var _this = this;
                _this.append(queryNode);
                if (queryNode.modality) {
                    _this.modalityQueries.push(queryNode);
                    _this.checkModalityQueries();
                    _this.modalityLoading.info = queryNode.title;
                    queryNode.on("loaded", function () {
                        _this.modalityQueries.splice(_this.modalityQueries.indexOf(queryNode), 1);
                        _this.checkModalityQueries();
                    });
                }

                return queryNode;

            }
        },
        "checkModalityQueries": {
            value: function () {
                var _this = this;
                if (_this.modalityQueries.length <= 0) {
                    _this.modalityLoading.close();
                } else {
                    _this.modalityLoading.open();
                }
            }
        },
        "open"                : {
            value: function () {
                var _this = this;
                _this.isOpened = true;
                _this.addClass("open");
            }
        },
        "close"               : {
            value: function () {
                var _this = this;
                _this.isOpened = false;
                _this.removeClass("open");
            }
        },
        "toggle"              : {
            value: function () {
                var _this = this;
                if (_this.isOpened) {
                    _this.close();
                } else {
                    _this.open();
                }
            }
        }
    });

    return _class;

});